package com.ssm.studen.controller;

import com.ssm.studen.eo.RoleEOPage;
import com.ssm.studen.model.Role;
import com.ssm.studen.service.RoleService;
import com.ssm.studen.tool.ResponseMessage;
import com.ssm.studen.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 对外提供数据服务接口
 */
@CrossOrigin
@Api(tags = "role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "角色添加方法")
    @RequestMapping(value = "/addRole", method = RequestMethod.GET)
    public int AddRole(Role role) {
        int IsAdd = 0;
        //传入数据的判断
        //（1）实体对象不能为空
        if (role != null) {
            //(2)角色名称不能为空
            if (role.getRname() != null) {
                //(3)添加角色数据
                IsAdd = roleService.AddRole(role);
            }
        }
        return IsAdd;
    }

    @ApiOperation(value = "角色添加方法")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ResponseMessage add(Role role) {
        int IsAdd = 0;
        //传入数据的判断
        //（1）实体对象不能为空
        if (role != null) {
            //(2)角色名称不能为空
            if (role.getRname() != null) {
                //(3)添加角色数据
                IsAdd = roleService.AddRole(role);
                if (IsAdd == 1) {
                    return Result.success("保存成功!");
                } else if (IsAdd == -1) {
                    return Result.error("角色重复不允许添加！");
                } else {
                    return Result.error("保存失败！");
                }
            }
        }
        return Result.error("保存失败");
    }


    @ApiOperation(value = "角色删除方法")
    @RequestMapping(value = "/delRole", method = RequestMethod.GET)
    public ResponseMessage delRole(int rid) {
        if (rid != 0) {
            int delInt = 0;
            delInt = roleService.DeleteRole(rid);
            if (delInt > 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } else {
            return Result.error("删除的主键不能为：0，请重新输入！");
        }
    }


    @ApiOperation(value = "角色修改方法")
    @RequestMapping(value = "/updateRol", method = RequestMethod.GET)
    public ResponseMessage updateRol(Role role) {
        //传入的参数不能为空
        if (role != null) {
            //参数当中主键不能为0
            if (role.getRid() != 0) {
                if (roleService.UpdateRole(role) == 1) {
                    //(role.is)
                    if(role.getRenable()!=null){
                        if (role.getRenable().length()<5){
                            roleService.UpdateRole(role);
                        }
                    }
                    return Result.success("更新成功");
                } else {
                    return Result.error("更新失败");
                }
            }
        }
        return Result.error("传入的参数不正确！");
    }


    @ApiOperation(value = "根据条件获取角色信息")
    @RequestMapping(value = "/getAll",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
    public ResponseMessage getAll(@RequestBody RoleEOPage roleEOPage){
        Map<String,List> map=new HashMap<>();
        List<RoleEOPage> listRole=roleService.getWhereAll(roleEOPage);
        map.put("list",listRole);
        return Result.success("200",map);
    }
}
